//
//  XMAccountSafeView.m
//  XMJJ_zml
//
//  Created by 丁浩 on 2025/5/21.
//

#import "XMAccountSafeView.h"

@interface XMAccountSafeView ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tbl;

@property (nonatomic, strong) XMUserInfoModel *userInfoModel;
@property (nonatomic, strong) NSArray *titleArr;

@end

@implementation XMAccountSafeView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self creatUI];
    }
    return self;
}

- (void)creatUI {
    [self addSubview:self.navView];
    self.navTitleLabel.text = @"账号与安全";
    
    self.backgroundColor = kColorHEX(0xF0F0F0);
    
    _titleArr = @[@[@"用户名", @"ID号", @"我的二维码名片", @"更换手机号", @"登录密码", @"实名认证"]/*, @[@"微信账号", @"QQ账号", @"微博账号"]*/];
    UITableView *tbl = [[UITableView alloc] initWithFrame:CGRectMake(0, kNavagationBarH, self.width, self.height - kNavagationBarH)];
    tbl.delegate = self;
    tbl.dataSource = self;
    tbl.backgroundColor = kColorHEX(0xF0F0F0);
    tbl.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
    tbl.separatorColor = kColorHEX(0xF1F1F1);
    if (@available(iOS 15.0, *)) {
        tbl.sectionHeaderTopPadding = 0;
    } else {
        tbl.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        // Fallback on earlier versions
    }
    _tbl = tbl;
    [self addSubview:tbl];
    
    UIView *footView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.width, KScaleWidth(110.0))];
    footView.backgroundColor = kColorHEX(0xF0F0F0);
    tbl.tableFooterView = footView;
    
    UIButton *logoutBtn = [UIButton buttonWithType:UIButtonTypeCustom font:KFontSize(16.0) frame:CGRectMake((self.width - KScaleWidth(320.0))/2, KScaleWidth(45.0), KScaleWidth(320.0), KScaleWidth(44.0)) title:@"注销账号" titleColor:kColorHEX(0xffffff)];
    [logoutBtn addTarget:self action:@selector(logoutAction:) forControlEvents:UIControlEventTouchUpInside];
    logoutBtn.backgroundColor = kColorHEX(0xD7D7D7);
    [logoutBtn setCornerRadius:logoutBtn.height/2];
    [footView addSubview:logoutBtn];
}

- (void)setContentWithUserInfoModel:(XMUserInfoModel *)userInfoModel {
    _userInfoModel = userInfoModel;
    [_tbl reloadData];
}

//注销登录
- (void)logoutAction:(UIButton *)button {
    self.cancelAccountBlock ? self.cancelAccountBlock() : nil;
}

#pragma mark - Delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return _titleArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSArray *subArr = _titleArr[section];
    return subArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return KScaleWidth(8.0);
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.width, KScaleWidth(8.0))];
    headView.backgroundColor = kColorHEX(0xF0F0F0);
    return headView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return KScaleWidth(44.0);
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellName = @"XMAccountSafeCellName";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellName];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellName];
        cell.textLabel.font = KFontSize(17.0);
        cell.textLabel.textColor = kColorHEX(0x333333);
        
        cell.detailTextLabel.font = KFontSize(15.0);
        cell.detailTextLabel.textColor = kColorHEX(0x666666);
    }
    if (indexPath.section == 0) {
        if (indexPath.row != 0 && indexPath.row != 1) {
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        }
        else {
            cell.detailTextLabel.text = [self.userInfoModel valueForKey:@[@"nickname", @"idNo"][indexPath.row]];
            cell.accessoryType = UITableViewCellAccessoryNone;
        }
    }
    else {
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    cell.textLabel.text = _titleArr[indexPath.section][indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    self.itemSelectBlock ? self.itemSelectBlock(indexPath.section, indexPath.row) : nil;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
